package com.demo.controller;

import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.demo.entity.UserEntity;
import com.demo.service.UserService;

@Controller
@AllArgsConstructor
public class HomeController {

	@Autowired
	private UserService userService;

	@GetMapping({ "/", "/index", "/home" })
	public String root() {
		return "index";
	}

	@GetMapping("/login")
	public String login() {
		return "login";
	}

	@GetMapping("/register")
	public String register() {
		return "register";
	}

	@PostMapping("/register")
	public String doRegister(UserEntity userEntity) {
		// 密码加密
		String bCryptPassword = new BCryptPasswordEncoder().encode(userEntity.getPassword());
		userEntity.setPassword(bCryptPassword);
		// 此处省略校验逻辑
		if (userService.insert(userEntity))
			return "redirect:register?success";
		return "redirect:register?error";
	}

}
